package org.osami.commons.smarthome.programnotifier;

import java.util.*;
import java.text.SimpleDateFormat;
//import org.osami.other.PushDialogService;
import org.osami.commons.dialog.IDialogService;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

class ProgramAlert extends TimerTask {
	
	private Timer timer = new Timer();
	private ProgramInfo info = null;
	private static SimpleDateFormat	shortTimeFormat = new SimpleDateFormat("dd.MM. HH:mm");
	private int questionId;
	
	public ProgramAlert(ProgramInfo info, Date fireTime) {
		
		this.info = info;
		//System.out.println("ProgramAlert: Alert added for program "+info.getName()+" and deviceID: "+info.getDeviceId());
		timer.schedule(this, fireTime);
	}
	
	public void run() {
		
		ServiceReference dialogRef = Activator.getBundleContext().getServiceReference(IDialogService.class.getName());
		if (null != dialogRef) {
			System.out.println("ProgramAlert: Sending program notification "+info.getName());
			IDialogService dialog = (IDialogService)Activator.getBundleContext().getService(dialogRef);
			questionId = dialog.createQuestion("Do you want to record program \"" + info.getName() + "\"" + " starting at " + shortTimeFormat.format(info.getStartTime()) +"?", new String [] {"Yes", "No"}, info.getDeviceId());
			UserAnswerHandler.instance().registerQuestion(questionId, info);
		} else {
			System.out.println("DialogService not found");
		}
		
	}

	
}
